_G.require("lib.win.CONST");
namespace win {
    export class user32 {
        /**信息框 */
        static messageBox(hwnd = 0, lpText = "提示", lpCaption = "标题", uType = 0x40) {
            return this.dll.MessageBoxA(hwnd, lpText, lpCaption, uType);
        }
        /**获取鼠标位置 */
        static getCursorPos() {
            let pos = ffi.new("POINT");
            this.dll.GetCursorPos(pos);
            return { x: pos.x, y: pos.y };
        }
        /**从坐标获取窗口句柄 */
        static windowFromPoint(x, y) {
            let pos = ffi.new("POINT");
            pos.x = x;
            pos.y = y;
            return this.dll.WindowFromPoint(pos);
        }
        /**
         * 枚举窗口句柄
         * @param f 回调函数(句柄,默认参数<无用>)
         */
        static enumWindows(f: (this: void, hwnd: number, l?: number) => void) {
            return this.dll.EnumWindows(f, 0);
        }
        /**获取窗口句柄标题 */
        static getWindowTextA(hwnd: number, len = 1024) {
            let buff = ffi.buff(len);
            this.dll.GetWindowTextA(hwnd, buff, len);
            return ffi.string(buff);
        }

        /**发送消息 */
        static sendMessage(hwnd: number, msg: number, wParam: any= 0, lParam: any = 0) {
            this.dll.SendMessageA(hwnd, msg, ffi.cast("void*", wParam), ffi.cast("void*", lParam));
        }
        /**发送消息 */
        static getWindowRect(hwnd: number) {
            let rect = ffi.new("RECT");
            this.dll.GetWindowRect(hwnd, rect);
            return rect as {
                top: number
                left: number
                right: number
                bottom: number
            };
        }
        static WNDCLASS() {
            let wndclass = ffi.new("WNDCLASSA[1]");
            return wndclass as {
                /**风格 */
                style: number,
                lpfnWndProc: (this: void, hwnd: number, message: number, wParam: number, lParam: number) => any
                cbClsExtra: number
                cbWndExtra: number
                hInstance: number
                lpszMenuName: string
                lpszClassName: string
            }
        }

    }

    export namespace user32 {
        ffi.cdef(
            `           
            typedef void *HANDLE;
            typedef void *LPVOID;
            typedef HANDLE HMODULE;
            typedef HANDLE HWND;
            typedef HANDLE HINSTANCE;
            typedef HANDLE HICON;
            typedef HICON HCURSOR;
            typedef HANDLE HBRUSH;
            typedef HANDLE HMENU;
            typedef unsigned char BYTE;
            typedef unsigned int UINT,*PUINT,*LPUINT;
            typedef  unsigned int UINT_PTR, *PUINT_PTR;
            typedef UINT_PTR WPARAM;
            typedef  long LONG_PTR, *PLONG_PTR;
            typedef LONG_PTR LPARAM;
            typedef LONG_PTR LRESULT;
            typedef unsigned long DWORD;
            int SendMessageA(int hwnd, int msg, void* wParam, void* lParam);
            typedef int WINBOOL,*PWINBOOL,*LPWINBOOL;
            typedef WINBOOL BOOL;
            typedef long LONG;
            typedef char CHAR;
            typedef struct tagPOINT {
                LONG x;
                LONG y;
            } POINT,POINTL,*PPOINT,*LPPOINT,*PPOINTL,*LPPOINTL;
            typedef LRESULT(*WNDPROC)(int,UINT,WPARAM,LPARAM);
            
            typedef const CHAR *LPCCH,*PCSTR,*LPCSTR;
            typedef struct _WNDCLASSA {
                UINT style;
                WNDPROC lpfnWndProc;
                int cbClsExtra;
                int cbWndExtra;
                HINSTANCE hInstance;
                HICON hIcon;
                HCURSOR hCursor;
                HBRUSH hbrBackground;
                LPCSTR lpszMenuName;
                LPCSTR lpszClassName;
            } WNDCLASSA,*LPWNDCLASSA,*PWNDCLASSA;
            typedef WNDCLASSA WNDCLASS,*LPWNDCLASS,*PWNDCLASS;
            typedef struct tagMSG {
                int hwnd;
                UINT message;
                WPARAM wParam;
                LPARAM lParam;
                DWORD time;
                POINT pt;
            } MSG,*LPMSG,*PMSG;
            HMODULE GetModuleHandleA(LPCSTR);
            HICON LoadIconA(HINSTANCE,LPCSTR);
            HCURSOR LoadCursorA(HINSTANCE,LPCSTR);
            HCURSOR LoadCursorFromFileA(LPCSTR);
            typedef void *HGDIOBJ;
            HGDIOBJ GetStockObject(int);
            typedef unsigned short WORD;
            typedef WORD ATOM;
            ATOM RegisterClassA(const WNDCLASSA*);
            int CreateWindowExA(DWORD,LPCSTR,LPCSTR,DWORD,int,int,int,int,int,HMENU,HINSTANCE,LPVOID);
            int GetMessageA(LPMSG,int,UINT,UINT);
            BOOL TranslateMessage(const MSG*);
            LONG DispatchMessageA(const MSG*);
            LRESULT DefWindowProcA(int,UINT,WPARAM,LPARAM);
            typedef struct tagRECT {
                LONG left;
                LONG top;
                LONG right;
                LONG bottom;
            } RECT,*PRECT,*LPRECT;
            typedef HANDLE HDC;
            typedef struct tagPAINTSTRUCT {
                HDC	hdc;
                BOOL fErase;
                RECT rcPaint;
                BOOL fRestore;
                BOOL fIncUpdate;
                BYTE rgbReserved[32];
            } PAINTSTRUCT,*LPPAINTSTRUCT;
            BOOL GetClientRect(int,LPRECT);
            typedef DWORD COLORREF;
            COLORREF SetTextColor(HDC,COLORREF);
            int SetBkMode(HDC,int);
            int DrawTextA(HDC,LPCSTR,int,LPRECT,UINT);
            HDC BeginPaint(int,LPPAINTSTRUCT);
            BOOL EndPaint(int,const PAINTSTRUCT*);
            void PostQuitMessage(int);
            HANDLE LoadImageA(HINSTANCE,LPCSTR,UINT,int,int,UINT);
            

            typedef struct {
                long x;
                long y;
            } POINT;
            typedef bool (__stdcall *WNDENUMPROC)(int hwnd, intptr_t l);
            typedef struct _DLONG {
                long wParam;
                long lParam;
            }stLONG;
            bool GetCursorPos(POINT* Point);
            int WindowFromPoint(POINT Point);
            int GetWindowTextA(int hWnd, str lpString, int nMaxCount);
            bool SetWindowTextA(int hWnd, str lpString);
            
            int MessageBoxA(int hWnd,str lpText,str lpCaption,int uType);
            int EnumWindows(WNDENUMPROC func, intptr_t l);
            bool IsZoomed(int hWnd);
            BOOL ShowWindow(int,int);
            BOOL UpdateWindow(int);
            BOOL MoveWindow( int hWnd, int X, int Y, int nWidth, int nHeight, BOOL bRepaint );

            typedef struct {
                int left;
                int top;
                int right;
                int bottom;
            } RECT;
            
            BOOL GetWindowRect(int hWnd,LPRECT lpRect);

            `
        )
        export let dll = ffi.load("user32");
        /**获取窗口句柄标题 */
        export let setWindowTextA = dll.SetWindowTextA as (this: void, hwnd: number, str: string) => any;
        /**创建窗口 */
        export let createWindowExA = dll.CreateWindowExA as (this: void,
            dwExStyle: number,
            lpClassName: string,
            lpWindowName: string,
            dwStyle: number,
            x: number,
            y: number,
            nWidth: number,
            nHeight: number,
            hWndParent: any,
            hMenu: any,
            hInstance: any,
            lpParam: any) => any;
        export let showWindow = dll.ShowWindow as (this: void, hwnd: number, nCmdShow: number) => boolean;
        export let updateWindow = dll.UpdateWindow as (this: void, hwnd: number) => boolean;
        export let getMessageA = dll.GetMessageA as (this: void, msg, hwnd: number, w: number, l: number) => number;
        export let translateMessage = dll.TranslateMessage as (this: void, msg) => boolean;
        export let dispatchMessageA = dll.DispatchMessageA as (this: void, msg) => boolean;
        export let defWindowProcA = dll.DefWindowProcA as (this: void, msg, h, w, l) => boolean;
        export let registerClassA = dll.RegisterClassA as (this: void, WNDCLASSA) => number;
        export let postQuitMessage = dll.PostQuitMessage as (this: void, n: number) => void;
        export let moveWindow = dll.MoveWindow as (this: void, hwnd, x, y, w, h, bRepaint: boolean) => void;
        //export let getWindowRect = dll.GetWindowRect as (this: void, hwnd: number, pRect: any) => void;


    }

}
